package com.qiantu.phone.ui.activity;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.hjq.base.FragmentPagerAdapter;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.RoomGalleryBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.manager.ActivityManager;
import com.qiantu.phone.ui.fragment.FloorGalleryFragment;
import com.qiantu.phone.widget.ViewPagerScrollIndicator;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class FloorGalleryActivity extends AppActivity {
    private ViewPagerScrollIndicator tabBar;
    private ViewPager viewPager;
    private FloorBean floorBean;
    private String selectPicUrl;
    private int type;
    @Override
    protected int getLayoutId() {
        return R.layout.room_gallery_activity;
    }

    @Override
    protected void initView() {
        type = getInt("type");
        tabBar = findViewById(R.id.tab_bar);
        viewPager = findViewById(R.id.view_pager);
        getTitleBar().getRightView().setEnabled(true);
    }

    @Override
    public void onRightClick(View view){
        if(type == 1){
            LLHttpManager.saveFloorPic(this,floorBean.getFloorSerialNo(),selectPicUrl,new HttpCallback<HttpData<Void>>(this) {
                @Override
                public void onStart(Call call) {
                    showDialog("请稍后");
                }

                @Override
                public void onSucceed(HttpData<Void> result) {
                    floorBean.setPicUrl(selectPicUrl);
                    LLFloorDBManager.getInstance(getContext()).put(floorBean);
                    EventBus.getDefault().post(new RefreshFloorRoomEvent());
                    finish();
                    ActivityManager.getInstance().finishActivity(RoomSettingActivity.class);
                }

                @Override
                public void onFail(Exception e) {
                    ToastUtils.show(e.getMessage());
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });
        }else{
            LLHttpManager.saveHousePic(this, AppApplication.getInstance().getCurrentHouseSerialNo(),selectPicUrl,new HttpCallback<HttpData<Void>>(this) {
                @Override
                public void onStart(Call call) {
                    showDialog("请稍后");
                }

                @Override
                public void onSucceed(HttpData<Void> result) {
                    floorBean.setPicUrl(selectPicUrl);
                    LLFloorDBManager.getInstance(getContext()).put(floorBean);
                    EventBus.getDefault().post(new RefreshFloorRoomEvent());
                    finish();
                    ActivityManager.getInstance().finishActivity(RoomSettingActivity.class);
                }

                @Override
                public void onFail(Exception e) {
                    ToastUtils.show(e.getMessage());
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });
        }
    }

    @Override
    protected void initData() {
        floorBean = LLFloorDBManager.getInstance(getContext()).getFloorByFloorSerialNo(getIntent().getStringExtra("floorSerialNo"));
        selectPicUrl = floorBean.getPicUrl();
            LLHttpManager.getFloorDefaultGallery(this, new HttpCallback<HttpData<List<RoomGalleryBean>>>(this) {
                @Override
                public void onStart(Call call) {
                    showDialog("请稍后");
                }

                @Override
                public void onSucceed(HttpData<List<RoomGalleryBean>> result) {
                    setGalleryData(result.getData().get(0).getImages());
                }

                @Override
                public void onFail(Exception e) {
                    ToastUtils.show(e.getMessage());
                }

                @Override
                public void onEnd(Call call) {
                    hideDialog();
                }
            });
    }

    public void setSelectPicUrl(String selectPicUrl){
        this.selectPicUrl = selectPicUrl;
    }

    public void setGalleryData(List<String> data){
        List<String> typeTitle = new ArrayList<>();
        FragmentPagerAdapter<Fragment> mPagerAdapter = new FragmentPagerAdapter<>(this);
        int index = 0;
        typeTitle.add("楼层");
        FloorGalleryFragment fragment = FloorGalleryFragment.newInstance(data,selectPicUrl);
        mPagerAdapter.addFragment(fragment);
        viewPager.setAdapter(mPagerAdapter);
        tabBar.setupViewpager(viewPager);
        tabBar.setTitleList(typeTitle);
        tabBar.selectTab(index);
        viewPager.setCurrentItem(index);
    }
}